package model

import (
	"context"
	"time"

	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/dao/schema"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logc"
	"gorm.io/gorm"
)

type UserLoginLogsModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewUserLoginLogsModel(ctx context.Context, db *gorm.DB) *UserLoginLogsModel {
	return &UserLoginLogsModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *UserLoginLogsModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *UserLoginLogsModel) InsertUserLoginLogs(data *schema.UserLoginLogs) error {
	data.CreatedAt = time.Now().Unix()
	data.UpdatedAt = time.Now().Unix()
	dbRes := model.getDb().Model(&schema.UserLoginLogs{}).Create(data)
	if err := dbRes.Error; err != nil {
		logc.Error(model.ctx, "UserLoginLogsModel-InsertUserLoginLogs,err:", err)
		return err
	}

	return nil
}
